package com.grapevine.contact.adapter;

import android.content.Context;
import android.database.Cursor;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.provider.ContactsContract;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CursorAdapter;
import android.widget.QuickContactBadge;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.grapevine.contact.R;
import com.grapevine.contact.local.NumberLocalProvider;
import com.grapevine.contact.util.AndroidPhoneLocationLoader;
import com.grapevine.contact.util.ContactPhotoLoader;

public class ContactAdapter extends CursorAdapter implements PinnedHeaderListView.PinnedHeaderAdapter
{
	private ContactPhotoLoader mContactPhotoLoader;
	private AndroidPhoneLocationLoader mAndroidPhoneLocationLoader;

	private int width;
	private int height;

	public ContactAdapter(Context context, Cursor c, ContactPhotoLoader contactPhotoLoader,
			AndroidPhoneLocationLoader loder)
	{
		super(context, c);
		mContactPhotoLoader = contactPhotoLoader;
		Drawable drawable = context.getResources().getDrawable(mContactPhotoLoader.mDefaultResourceId);
		width = drawable.getMinimumWidth() + 15;
		height = drawable.getMinimumHeight() + 15;
		mAndroidPhoneLocationLoader = loder;
	}

	@Override
	public void bindView(View view, Context context, Cursor cursor)
	{

		QuickContactBadge photo = (QuickContactBadge) view.findViewById(R.id.contact_photo);

		photo.setLayoutParams(new RelativeLayout.LayoutParams(width, height));

		TextView name = (TextView) view.findViewById(R.id.contact_name);
		TextView num = (TextView) view.findViewById(R.id.contact_number);

		TextView local = (TextView) view.findViewById(R.id.contact_local);

		final int id = cursor.getInt(cursor.getColumnIndex(ContactsContract.Contacts._ID));

		String nameText = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));

		String number = "";

		Cursor phones = context.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
				ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + id, null, null);
		if (phones.moveToFirst())
		{
			number = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
			number = number.replaceAll("-", "");
		}
		phones.close();
		name.setText(nameText);
		num.setText(number);
		int photoID = cursor.getInt(cursor.getColumnIndex(ContactsContract.Contacts.PHOTO_ID));
		photo.assignContactUri(Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_URI, String.valueOf(id)));

		Cursor mLocation = context.getContentResolver().query(NumberLocalProvider.CONNECT_URI, null,
				NumberLocalProvider.NUMBER + "='" + number + "'", null, null);

		if (mLocation.moveToFirst())
		{
			String provice = mLocation.getString(mLocation.getColumnIndex(NumberLocalProvider.PROVINCE));
			String city = mLocation.getString(mLocation.getColumnIndex(NumberLocalProvider.CITY));
			String location = provice + "  " + city;
			local.setText(location);
		}
		else
		{
			mAndroidPhoneLocationLoader.loadLocation(local, number);
		}
		mLocation.close();
		mContactPhotoLoader.loadPhoto(photo, photoID);
	}

	@Override
	public View newView(Context context, Cursor cursor, ViewGroup parent)
	{
		View convertView = LayoutInflater.from(context).inflate(R.layout.contact_item, null);
		return convertView;
	}

	@Override
	public void configurePinnedHeader(View header, int position, int alpha)
	{
		// TODO Auto-generated method stub

	}

	@Override
	public int getPinnedHeaderState(int position)
	{
		// TODO Auto-generated method stub
		return 0;
	}

}